#include<conio.h>//不按回车就可控制
#include<time.h>//时间函数
#include<stdlib.h>//清屏函数
#include<stdio.h>
#include<windows.h>
int position_x, position_y;
int high, width;
int enemy_x[5];
int enemy_y[5];
int speed;
float startTime, endTime;//初始时间，最后时间
void gotoxy(int a, int b)
{
	HANDLE hout;
	COORD coord;


	coord.X = a;
	coord.Y = b;


	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hout, coord);
}
void HideCursor()//清除光标的函数
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//隐藏输入符号
}
void start_time()
{
	startTime = clock();
	
}//记录开始时间
void  now_time()/*记录现在时间*/
{
	endTime = clock();
	
}
void startup()
{
	speed = 0;
	high = 40;
	width = 40;
	position_x = rand() % (high - 1) + 1;
	position_y = rand() % (width - 1) + 1;
	for (int k = 0; k < 5; k++)
	{
		enemy_x[k] = rand() % (high - 1) + 1;
		enemy_y[k] = rand() % (width - 1) + 1;//随机敌人初始位置坐标
	}
	start_time();
	HideCursor();


}
void show()
{
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i < high; i++)
	{
		for (j = 0; j < width; j++)
		{
			if ((i == position_x) && (j == position_y))
				printf("*");
			else
				printf(" ");
			for (int k = 0; k < 5; k++)
			{
				if (i == enemy_x[k] && j == enemy_y[k])
					printf("@");
			}
		}
		printf("\n");
	}
	now_time();
	printf("\n");
	printf("%.3f s\n", (endTime - startTime) / 1000.0);
}

void updateWithoutInput()
{

	if (speed < 10)
		speed++;
	if(speed==10)
		{
		int k;
			for ( k = 0; k < 5; k++)
			{
				if (enemy_x[k] > position_x)
					enemy_x[k]--;
				else if (enemy_x[k] < position_x)
					enemy_x[k]++;
			}
			for ( k = 0; k < 5; k++)
			{
				if (enemy_y[k] > position_y)
					enemy_y[k]--;
				else if (enemy_y[k] < position_y)
					enemy_y[k]++;
			}
			for ( k = 0; k < 5; k++)
			{
				if (enemy_x[k] == position_x && enemy_y[k] == position_y)
				{
					printf(" you loser!");
		
					exit(0);
				}
			
			}
			speed = 0;
		}
	

}



void updateWithInput()
{
	char input;

	if (_kbhit())
	{
		input = _getch();
		if (input == 'a')
			position_y--;
		if (input == 'd')
			position_y++;
		if (input == 'w')
			position_x--;
		if (input == 's')
			position_x++;
		if (input == 27)//按esc暂停
			system("pause");
		if (position_x <= 0)
			position_x = 0;
		if (position_x >= high)
			position_x = high;
		if (position_y >= width)
			position_y = width;
		if (position_y <= 0)
			position_y = 0;
	}
}

void main()
{
	startup();
	while (1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}

}
